package com.example.foodiemoments

import android.content.Intent
import android.os.Bundle
import android.util.Patterns
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.foodiemoments.databinding.ActivityRegisterBinding

class RegisterActivity : AppCompatActivity() {

    private lateinit var binding: ActivityRegisterBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupViews()
    }

    private fun setupViews() {
        // 返回按钮
        binding.btnBack.setOnClickListener {
            finish()
        }

        // 注册按钮
        binding.btnRegister.setOnClickListener {
            val username = binding.etUsername.text.toString().trim()
            val email = binding.etEmail.text.toString().trim()
            val phone = binding.etPhone.text.toString().trim()
            val password = binding.etPassword.text.toString().trim()
            val confirmPassword = binding.etConfirmPassword.text.toString().trim()

            // 清除之前的错误
            clearErrors()

            // 验证输入
            if (validateInput(username, email, phone, password, confirmPassword)) {
                performRegister(username, email, phone, password)
            }
        }

        // 已有账号，去登录
        binding.tvGoLogin.setOnClickListener {
            finish()
        }

        // 用户协议
        binding.tvUserAgreement.setOnClickListener {
            Toast.makeText(this, "查看用户协议", Toast.LENGTH_SHORT).show()
        }

        // 隐私政策
        binding.tvPrivacyPolicy.setOnClickListener {
            Toast.makeText(this, "查看隐私政策", Toast.LENGTH_SHORT).show()
        }
    }

    private fun clearErrors() {
        binding.tilUsername.error = null
        binding.tilEmail.error = null
        binding.tilPhone.error = null
        binding.tilPassword.error = null
        binding.tilConfirmPassword.error = null
    }

    private fun validateInput(
        username: String,
        email: String,
        phone: String,
        password: String,
        confirmPassword: String
    ): Boolean {
        // 验证用户名
        if (username.isEmpty()) {
            binding.tilUsername.error = "请输入用户名"
            return false
        }
        if (username.length < 3) {
            binding.tilUsername.error = "用户名至少3个字符"
            return false
        }

        // 验证邮箱
        if (email.isEmpty()) {
            binding.tilEmail.error = "请输入邮箱"
            return false
        }
        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            binding.tilEmail.error = "邮箱格式不正确"
            return false
        }

        // 验证手机号
        if (phone.isEmpty()) {
            binding.tilPhone.error = "请输入手机号"
            return false
        }
        if (!phone.matches(Regex("^1[3-9]\\d{9}$"))) {
            binding.tilPhone.error = "手机号格式不正确"
            return false
        }

        // 验证密码
        if (password.isEmpty()) {
            binding.tilPassword.error = "请输入密码"
            return false
        }
        if (password.length < 6) {
            binding.tilPassword.error = "密码至少6位"
            return false
        }

        // 验证确认密码
        if (confirmPassword.isEmpty()) {
            binding.tilConfirmPassword.error = "请确认密码"
            return false
        }
        if (password != confirmPassword) {
            binding.tilConfirmPassword.error = "两次密码不一致"
            return false
        }

        // 验证用户协议
        if (!binding.cbAgreeTerms.isChecked) {
            Toast.makeText(this, "请阅读并同意用户协议和隐私政策", Toast.LENGTH_SHORT).show()
            return false
        }

        return true
    }

    private fun performRegister(username: String, email: String, phone: String, password: String) {
        // TODO: 实现实际的注册逻辑
        Toast.makeText(
            this,
            "注册成功！\n欢迎加入美食大家庭 🎉\n用户名: $username",
            Toast.LENGTH_LONG
        ).show()

        // 注册成功后返回登录页
        finish()
    }
}
